// Copyright 2025 CCC
//
// Licensed under the Apache License, Version 2.0 (the 'License')
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an 'AS IS' BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/***
 * @description: 将对象添加当作参数拼接到URL上面
 * @param {string} baseUrl 需要拼接的url
 * @param {object} obj 对象参数
 * @return {string} 处理后的url
 * 例子：
 *let obj = {a:1,b:2,c:3}
 *setObjToUrlParams('www.baidu.com', obj)
 * ==>www.baidu.com?a=3&b=4
 */
export function setObjToUrlParams(baseUrl: string, obj: any): string {
  let parameters = '';
  let url = '';

  for (const key in obj) {
    parameters += key + '=' + encodeURIComponent(obj[key]) + '&';
  }

  parameters = parameters.replace(/&$/, '');

  if (/\?$/.test(baseUrl)) {
    url = baseUrl + parameters;
  } else {
    url = baseUrl.replace(/\/?$/, '/?') + parameters;
  }

  return url;
}
